package com.coollang.smartbra.adapter;

import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.coollang.smartbra.R;
import com.coollang.smartbra.beans.NewActionData;
import com.coollang.smartbra.utils.ActionTypeParse;
import com.coollang.smartbra.utils.TimeUtils;

import java.util.List;

/**
 * @author : Timmy
 * @data : 2017/12/19
 * @Fuction :
 */
public class NewTimeActionDetailsAdapter extends BaseMultiItemQuickAdapter<NewActionData, BaseViewHolder> {

    public NewTimeActionDetailsAdapter(List<NewActionData> data) {
        super(data);
        addItemType(NewActionData.ACTION_CORRECT_FLAG, R.layout.item_pbzc_details_corrent);
        addItemType(NewActionData.ACTION_ERROR_FLAG, R.layout.item_pbzc_details_error);
    }

    @Override
    protected void convert(BaseViewHolder holder, NewActionData item) {
        int itemViewType = holder.getItemViewType();

        int itemCount = getItemCount();
        int position = holder.getAdapterPosition();
        int currentPosition = itemCount - position;

        String currentPositionSt = "" + currentPosition;
        if ((currentPosition) < 10) {
            currentPositionSt = "0" + currentPosition;
        }

        holder.setText(R.id.tv_position, currentPositionSt)
                .setText(R.id.tv_time, TimeUtils.getFormatmillisecond(item.getTime()));

        if (itemViewType == NewActionData.ACTION_CORRECT_FLAG) {
            parseCorrectInfo(holder, item);
        } else if (itemViewType == NewActionData.ACTION_ERROR_FLAG) {
            parseErrorInfo(holder, item);
        }
    }

    /**
     * 解析正确的时间数据
     */
    private void parseCorrectInfo(BaseViewHolder holder, NewActionData item) {

        int itemCount = getItemCount();
        int position = holder.getAdapterPosition();

        List<NewActionData> data = getData();

        TextView tvCurrentTime = holder.getView(R.id.tv_current_time);
        if (itemCount != position + 1) {
            int time1 = data.get(position).getTime();
            int time2 = data.get(position + 1).getTime();

            int currentTime = time1 - time2;

            tvCurrentTime.setText("+ " + TimeUtils.getFormatmillisecond(currentTime));
        } else {
            int time = item.getTime();
            tvCurrentTime.setText("+ " + TimeUtils.getFormatmillisecond(time));
        }


    }

    /**
     * 解析错误的时间数据
     */
    private void parseErrorInfo(BaseViewHolder holder, NewActionData item) {
        // 错误
        String errorInfo = ActionTypeParse.parseErrorType(item.getActionType(), item.getErrorType());

        int position = holder.getAdapterPosition();
        holder.setText(R.id.tv_error_info, errorInfo);

        TextView tvCurrentTime = holder.getView(R.id.tv_currtime);

        if (position != 0) {
            List<NewActionData> data = getData();
            if (position + 1 != data.size()) {
                int time = data.get(position).getTime();
                int time1 = data.get(position + 1).getTime();
                int currentTime = time - time1;
                tvCurrentTime.setText(TimeUtils.getFormatmillisecond(currentTime));
            } else {
                int time = data.get(position).getTime();
                tvCurrentTime.setText(TimeUtils.getFormatmillisecond(time));
            }
        }
    }
}
